package ro.dandanciu.webgallery.domainmodel;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import ro.dandanciu.webgallery.domainmodel.utils.DbConstants;

/**
 * Domain model class for images.
 * @author Dan
 */
@Entity
@Table(name = "TP_IMAGE")
@NamedQueries({
        @NamedQuery(name = "Image.GetByGalleryId",
                query = "select i from Image i where i.parent.id = :galleryId"),
        @NamedQuery(name = "Image.DeleteByGalleryIdAndFileName",
                query = "delete from Image i where i.parent.id = :galleryId and name = :name ") })
public class Image {

    /**
     * The identifier of the image.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "K_IMAGE")
    private long id;

    /**
     * The name of the image.
     */
    @Column(name = "C_NAME", length = DbConstants.Image.NAME_SIZE)
    private String name;

    /**
     * The content type of the image.
     */
    @Column(name = "C_TYPE", length = DbConstants.Image.TYPE_SIZE)
    private String contentType;

    /**
     * A description of the image.
     */
    @Column(name = "N_SIZE", length = DbConstants.Image.DESCRIPTION_SIZE)
    private long size;

    /**
     * Parent gallery, if null, then is root gallery.
     */
    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name = "F_PARENT")
    private Gallery parent;

    /**
     * The timestamp of the last time this item was updated. Mapped to
     * D_UPDATED.
     */
    @Column(name = "D_UPDATED")
    @Temporal(value = TemporalType.TIMESTAMP)
    private Date updated;

    /**
     * The user id/software which has performed the last update. Mapped to
     * C_UPDATER.
     */
    @Column(name = "C_UPDATER", length = DbConstants.Generic.MODIFIED_SIZE)
    private String updater;

    /**
     * Returns the string representation of this object.
     * @return string representation of this object.
     */
    public String toString() {
        return "Image(@id=" + id + ", name=" + name + ")";
    }

    /**
     * @return the id
     */
    public final long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public final void setId(final long id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public final String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public final void setName(final String name) {
        this.name = name;
    }

    /**
     * @return the contentType
     */
    public final String getContentType() {
        return contentType;
    }

    /**
     * @param contentType the contentType to set
     */
    public final void setContentType(final String contentType) {
        this.contentType = contentType;
    }

    /**
     * @return the size
     */
    public final long getSize() {
        return size;
    }

    /**
     * @param size the size to set
     */
    public final void setSize(final long size) {
        this.size = size;
    }

    /**
     * @return the parent
     */
    public final Gallery getParent() {
        return parent;
    }

    /**
     * @param parent the parent to set
     */
    public final void setParent(final Gallery parent) {
        this.parent = parent;
    }

    /**
     * @return the updated
     */
    public final Date getUpdated() {
        return updated;
    }

    /**
     * @param updated the updated to set
     */
    public final void setUpdated(final Date updated) {
        this.updated = updated;
    }

    /**
     * @return the updater
     */
    public final String getUpdater() {
        return updater;
    }

    /**
     * @param updater the updater to set
     */
    public final void setUpdater(final String updater) {
        this.updater = updater;
    }
}
